using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.IO;

using System.Configuration;

using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using Csla.Data;
namespace buildingLib.daily
{
    [Serializable()]
    public class RasterFiles:BusinessListBase<RasterFiles,RasterFile>
    {
        private RasterFiles() { }
        internal static RasterFiles NewRasterFiles()
        {
            return new RasterFiles();
        }
        public static RasterFiles GetRasterFiles(Guid saveid)
        {
            return DataPortal.Fetch<RasterFiles>(new Criteria(saveid));
        }

        public void AddRasterFile(string url)
        {                        
            this.Add(new RasterFile(url));
        }

        internal void Update(Archive archive)
        {           
            this.RaiseListChangedEvents = false;
            foreach (RasterFile obj in DeletedList)
                obj.DeleteSelf();
            DeletedList.Clear();
            
            foreach (RasterFile obj in this)
            {               
                if (obj.IsNew)
                    obj.Insert(archive.ID);
                else
                    obj.Update();
            }            
            this.RaiseListChangedEvents = true;
        }

        [Serializable()]
        private class Criteria
        {
            private Guid _id;
            public Guid Id
            {
                get { return _id; }
            }

            public Criteria(Guid id)
            {
                _id = id;
            }
        }

        private void DataPortal_Fetch(Criteria criteria)
        {
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetSqlStringCommand("select * from rasterfile where saveid ='"+criteria.Id.ToString()+"'");

            using (SafeDataReader dr = new SafeDataReader(db.ExecuteReader(dbcom)))
            {
                while (dr.Read())
                {
                    this.Add(new RasterFile(dr));
                }
            }
        }
    }
}
